package com.lauwerens.kaku_proxy;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
//import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
//import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	
	private static final int MY_BUTTON = 9000; 
	private static final int MY_BUTTONX = 9001;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// Toast.makeText(this, "Start !", Toast.LENGTH_LONG).show();
		
		LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
		String[] devices = {"Voorkamer AAN",
							"Voorkamer UIT",
							"Achterkamer AAN",
							"Achterkamer UIT",
							"Spots AAN",
							"Spots UIT",
							"Water AAN",
							"Water UIT",
							"Schutting AAN",
							"Schutting UIT"	};
		
		for(int i=0; i < devices.length; i++){
			 Button b = new Button(this);
		     b.setText(devices[i]);
		     b.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		     b.setId(3000 + i);
		     b.setOnClickListener(this);
		     ll.addView(b);
		}
       	    
		
	    //final ListView listview = (ListView) findViewById(R.id.devicelistview);
	    //String[] values = new String[] { "R1D1F0", "R1D1F1", "R1D2F0",
	    //    "R1D2F1" };

	    //final ArrayList<String> list = new ArrayList<String>();
	    //for (int i = 0; i < values.length; ++i) {
	    //  list.add(values[i]);
	    //}
	    //final StableArrayAdapter adapter = new StableArrayAdapter(this,
	    //    android.R.layout.simple_list_item_1, list);
	    //listview.setAdapter(adapter);
//
	//    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
	//      @Override
	  //    public void onItemClick(AdapterView<?> parent, final View view,
	    //      int position, long id) {
	    //    final String item = (String) parent.getItemAtPosition(position);
	    //    //view.animate().setDuration(2000).alpha(0);
	    //    //list.remove(item);
        //    //adapter.notifyDataSetChanged();
	    //    SendCommandToProxy(view , item.toString());
            
            //view.setAlpha(1);
	     // }

	   // });
		
	}

	 public void onClick(View v) {
	        Toast toast;
	        Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
	        switch (v.getId()) {
	        case MY_BUTTON:
	            toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
	            toast.setGravity(Gravity.TOP, 25, 400);
	            toast.show();
	            break;
	        case 3000:
	        	SendCommandToProxy(v , "R1D1F1");
	            break;
	        case 3001:
	        	SendCommandToProxy(v , "R1D1F0");
	            break;
	        case 3002:
	        	SendCommandToProxy(v , "R1D2F1");
	            break;
	        case 3003:
	        	SendCommandToProxy(v , "R1D2F0");
	            break;
	        case 3004:
	        	SendCommandToProxy(v , "R2D1F1");
	            break;
	        case 3005:
	        	SendCommandToProxy(v , "R2D1F0");
	            break;
	        case 3006:
	        	SendCommandToProxy(v , "R2D2F1");
	            break;
	        case 3007:
	        	SendCommandToProxy(v , "R2D2F0");
	            break;
	        case 3008:
	        	SendCommandToProxy(v , "R2D3F1");
	            break;
	        case 3009:
	        	SendCommandToProxy(v , "R2D3F0");
	            break;
	            
	            // More buttons go here (if any) ...
	        }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private class StableArrayAdapter extends ArrayAdapter<String> {

	    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

	    public StableArrayAdapter(Context context, int textViewResourceId,
	        List<String> objects) {
	      super(context, textViewResourceId, objects);
	      for (int i = 0; i < objects.size(); ++i) {
	        mIdMap.put(objects.get(i), i);
	      }
	    }

	    @Override
	    public long getItemId(int position) {
	      String item = getItem(position);
	      return mIdMap.get(item);
	    }

	    @Override
	    public boolean hasStableIds() {
	      return true;
	    }

	  }

	
	private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
	    @Override
	    protected String doInBackground(String... urls) {
	      String response = "";
	      for (String url : urls) {
	        DefaultHttpClient client = new DefaultHttpClient();
	        HttpGet httpGet = new HttpGet(url);
	        try {
	          HttpResponse execute = client.execute(httpGet);
	          InputStream content = execute.getEntity().getContent();

	          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
	          String s = "";
	          while ((s = buffer.readLine()) != null) {
	            response += s;
	          }

	        } catch (Exception e) {
	          e.printStackTrace();
	        }
	      }
	      return response;
	    }

	    @Override
	    protected void onPostExecute(String result) {
	      //Toast.makeText(getApplicationContext(), result , Toast.LENGTH_LONG).show();
	      //textView.setText(result);
	    }
	  }

	  public void SendCommandToProxy(View view, String command) {
	    String url;
		DownloadWebPageTask task = new DownloadWebPageTask();
	    //task.execute(new String[] { "http://192.168.0.197/klik/index.php?fun=sezd&arg=000,!R1D2F0|Test|uit." });
	    url = "http://192.168.0.197/klik/index.php?fun=send&arg=000,!";
	    url += command;
	    url += "%7CTest%7Cuit.";
	    //Toast.makeText(getApplicationContext(), url , Toast.LENGTH_LONG).show();
	    task.execute(new String[] { url});

	  }
}
